<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('新增入庫')"/>
    <th:block th:include="include :: select2-css"/>
</head>
<body>
<div class="main-content">
    <form id="form-user-add" class="form-horizontal">
        <h4 class="form-header h4">入庫單信息</h4>
        <div class="row">
            <div class="col-sm-4">
                <div class="form-group">
                    <label class="col-sm-4 control-label is-required">入庫人員：</label>
                    <div class="col-sm-8">
                        <div class="input-group">
                            <input name="manage" placeholder="请输入入庫人員" class="form-control" type="text" maxlength="30"
                                   required>
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-sm-4">
                <div class="form-group">
                    <label class="col-sm-4 control-label is-required">接收人：</label>
                    <div class="col-sm-8">
                        <div class="input-group">
                            <input id="receive" name="receive" class="form-control" type="text" maxlength="50"
                                   placeholder="请输入接收人">
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-sm-4">
                <div class="form-group">
                    <label class="col-sm-4 control-label is-required">樓層：</label>
                    <div class="col-sm-8">
                        <div class="input-group">
                            <input id="floor" name="floor" class="form-control" type="text" maxlength="50"
                                   placeholder="请输入樓層">
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <h4 class="form-header h4">邊角料信息信息</h4>
        <div>
            <div class="table-box" style="margin: 20px;">
                <div id="toolbar">
                    <a id="addRow" onclick="addRow()">添加行</a>
                    <a id="removeRow" onclick="removeRow()">删除行</a>
                    <!--                    <a id="getData" onclick="getData()">獲取數據</a>-->
                </div>
                <table id="exampleTable"></table>
            </div>
        </div>
    </form>
</div>

<div class="row">
    <div class="col-sm-offset-5 col-sm-10">
        <button type="button" class="btn btn-sm btn-primary" onclick="submitHandler()"><i class="fa fa-check"></i>保 存
        </button>&nbsp;
        <button type="button" class="btn btn-sm btn-danger" onclick="closeItem()"><i class="fa fa-reply-all"></i>关 闭
        </button>
    </div>
</div>
<th:block th:include="include :: footer"/>
<th:block th:include="include :: select2-js"/>
<script>
    var prefix = ctx + "system/leftoverIn";
    var ind = 0;

    $("#form-user-add").validate({
        onkeyup: false,
        rules: {
            floor: {
                floor: true,
            }
        }
    });


    $(function () {

        $('#exampleTable').bootstrapTable({
            // url: '',//如果需要请求后台获取初始数据的话，这里写请求地址
            // toolbar: '#toolbar',
            // clickEdit: true,
            // showToggle: true,
            // pagination: true,       //显示分页条
            // showColumns: true,
            // showPaginationSwitch: true,     //显示切换分页按钮
            // showRefresh: true,      //显示刷新按钮
            // //clickToSelect: true,  //点击row选中radio或CheckBox
            rules: {
                receive: {
                    floor: true
                }
            },
            columns: [{
                checkbox: true
            },
                {
                    field: 'index',
                    title: '序號',
                    formatter: function (value, row, index) {
                        return row.index = index + 1; //返回行号
                    }
                }, {
                    field: 'leftoverId',
                    title: '邊角料料號',
                    formatter: function (value, row, index) {
                        var result = "<input id='" + index + "leftoverId' placeholder='邊角料料號'" +
                            "class='form-control'  value='" + (value == undefined ? '' : value) + "' onclick='selectLeftoverTree(" + index + ")' onblur='getValues(" + index + ")'>";
                        return result //formatter 这里，里面的value一定要加，不然写值的时候会写不上去
                    }
                },
                // {
                //     field: 'floor',
                //     title: '樓層',
                //     formatter: function (value, row, index) {
                //         var result = "<input id='floor" + index + "' placeholder='樓層'" +
                //             "class='form-control' value='" + (value == undefined ? '' : value) + "' onblur='getValues(" + index + ")'>";
                //         return result
                //     }
                // },
                {
                    field: 'proportion',
                    title: '脫脂脫水比例',
                    formatter: function (value, row, index) {
                        var result = "<input id='" + index + "proportion' placeholder='脫脂脫水比例'" +
                            "class='form-control' value='" + (value == undefined ? '' : value) + "' onblur='getValues(" + index + ")'>";
                        return result
                    }
                }, {
                    field: 'inNum',
                    title: '入庫數量',
                    formatter: function (value, row, index) {
                        var result = "<input id='" + index + "inNum' placeholder='入庫數量'" +
                            "class='form-control' value='" + (value == undefined ? '' : value) + "' onblur='getValues(" + index + ")'>";
                        return result
                    }
                }, {
                    field: 'unit',
                    title: '單位',
                    formatter: function (value, row, index) {
                        // var result = "<select id='" + index + "unit'" + "class='form-control m-b'>"
                        //     + "<option value='G'>克</option>"
                        //     + "<option value='KG'>千克</option>"
                        //     + "<option value='T'>噸</option>" +
                        //     "</select>";
                        var result = "<input id='" + index + "unit' placeholder='千克'" +
                            "class='form-control' value='" + (value == undefined ? 'KG' : value) + "' onblur='getValues(" + index + ")' disabled>";
                        return result
                    }
                }]
        });
        //添加行

        addRow();

    })

    function addRow() {
        var row =
            {
                "leftoverId": "",
                "floor": "",
                "proportion": "",
                "inNum": "",
                "unit": ""
            };
        //append  追加到最后一行
        //prepend  新增到第一行
        $('#exampleTable').bootstrapTable('append', row);
        //更新行数据
        $('#exampleTable').bootstrapTable('updateRow', row);
        //定位到最后一行
        $('#exampleTable').bootstrapTable('scrollTo', 'bottom');
    }

    function getData() {
        alert(JSON.stringify($('#exampleTable').bootstrapTable('getData')));

    }

    function getValues(index) {
        var rows = $('#exampleTable').bootstrapTable('getData');
        $.each(rows, function (i, row) {
            if (row.index == (index + 1)) {
                row.leftoverId = $("#" + index + "leftoverId").val();
                row.floor = $("#floor" + index).val();
                row.proportion = $("#" + index + "proportion").val();
                row.inNum = $("#" + index + "inNum").val();
                row.unit = $("#" + index + "unit").val();
                $('#exampleTable').bootstrapTable('updateRow', row);
                return false;
            }
        });
    }

    function removeRow() {
        var rows = $('#exampleTable').bootstrapTable('getSelections');//获取选中行
        if (rows.length == 0) {
            layer.msg("请选择要删除的数据");
            return;
        }
        var indexs = [];
        for (var i = 0; i < rows.length; i++) {
            indexs[i] = rows[i].index;
        }
        //删除
        $('#exampleTable').bootstrapTable('remove', {
            field: 'index',
            values: indexs
        });
    }

    $("#form-user-add").validate({
        onkeyup: false,
        rules: {},
        focusCleanup: true
    });

    function submitHandler() {
        var data = $("#form-user-add").serializeArray();
        var leftoverIn = $('#exampleTable').bootstrapTable('getData');
        if ($.validate.form()) {
            // var manage = $("input[id='manage']").var;
            // var receive = $("input[id='receive']").var;
            data.push({"name": "leftoverIn", "value": JSON.stringify(leftoverIn)});
            // data.push({"name": "manage", "value": manage});
            // data.push({"name": "receive", "value": receive});
            $.operate.saveTab(prefix + "/add", data);
        }
    }

    function selectLeftoverTree(i) {
        var url = ctx + "system/leftover/selectLeftoverId";
        ind = i;
        var options = {
            title: '选择边角料料号',
            width: "380",
            url: url,
            callBack: doSubmit
        };
        $.modal.openOptions(options);
    }

    function doSubmit(index, layero, i) {
        var tree = layero.find("iframe")[0].contentWindow.$._tree;
        var body = $.modal.getChildFrame(index);
        $("#" + ind + "leftoverId").val(body.find('#treeName').val());
        getValues(ind);
        $.modal.close(index);
    }

    $(function () {
        $('#post').select2({
            placeholder: "请选择岗位",
            allowClear: true
        });
    })
</script>
</body>
</html>